package com.rxt.thermopro.adapter

import android.annotation.SuppressLint
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.rxt.thermopro.R
import com.rxt.thermopro.app.LocalDataDao
import com.rxt.thermopro.bean.ProfileBean
import com.rxt.thermopro.ui.fragment.SetProfileFragment
import kotlinx.android.synthetic.main.item_view_set_profile.view.*
import kotlinx.android.synthetic.main.item_view_set_profile_bottom_add.view.*

class ProfileListAdapter(var list: MutableList<ProfileBean>) :
    RecyclerView.Adapter<ProfileListAdapter.MyViewHolder>() {

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    }

    companion object {
        const val VIEW_TYPE_ITEM = 1
        const val VIEW_TYPE_BOTTOM_ADD = 2
    }

    var presenter: SetProfileFragment.ProfilePresenter? = null

    fun setProfilePresenter(presenter: SetProfileFragment.ProfilePresenter) {
        this.presenter = presenter
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView =
            if (viewType == VIEW_TYPE_ITEM) {
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_view_set_profile, parent, false)
            } else {
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_view_set_profile_bottom_add, parent, false)
            }
        return MyViewHolder(itemView)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        if (getItemViewType(position) == VIEW_TYPE_BOTTOM_ADD) {
            holder.itemView.addView.setOnClickListener {
                presenter?.onItemClickAdd(holder.itemView, position)
            }
        } else {
            holder.itemView.viewProfileName.text = list[position].name
            when {
                list[position].profileValue.contains("~") -> {
                    if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_F && list[position].highValueF.isNotEmpty()) {
                        holder.itemView.viewProfileValue.text =
                            list[position].lowValueF +
                                    "~" + list[position].highValueF +
                                    LocalDataDao.INSTANCE.getTempUnit()
                    } else {
                        holder.itemView.viewProfileValue.text =
                            LocalDataDao.INSTANCE.convertUnitNum(list[position].lowTemperatureNum) +
                                    "~" + LocalDataDao.INSTANCE.convertUnitNum(list[position].highTemperatureNum) +
                                    LocalDataDao.INSTANCE.getTempUnit()
                    }
                }
                list[position].profileValue.contains(" ") &&
                        list[position].itemValues.isNotEmpty() -> {
                    val temp = list[position].profileValue.split(" ").last()
                    val currentItem = list[position].itemValues.first { it.checked }

                    if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_F && currentItem.itemValueF.isNotEmpty()) {
                        holder.itemView.viewProfileValue.text = list[position].profileValue.replace(
                            temp,
                            currentItem.itemValueF
                        ) + LocalDataDao.INSTANCE.getTempUnit()
                    } else {
                        holder.itemView.viewProfileValue.text = list[position].profileValue.replace(
                            temp,
                            LocalDataDao.INSTANCE.convertUnit(currentItem.itemValue)
                        )
                    }
                }
                else -> {
//                    holder.itemView.viewProfileValue.text =
//                        LocalDataDao.INSTANCE.convertUnit(list[position].profileValue)
                    holder.itemView.viewProfileValue.text =
                        LocalDataDao.INSTANCE.formatHighValueUnit(list[position])
                }
            }

            holder.itemView.checkView.isChecked = list[position].checked
            holder.itemView.imageView.isVisible = list[position].itemValues.isNotEmpty()
            holder.itemView.itemIcon.setImageResource(list[position].iconRes)

            holder.itemView.setOnClickListener {
                presenter?.onItemClick(holder.itemView, position)
            }

            holder.itemView.viewProfileValue.setOnClickListener {
                presenter?.onItemSelectClick(holder.itemView, position)
            }

            holder.itemView.checkView.setOnClickListener {
                list[position].checked = !list[position].checked
                presenter?.onItemChecked(holder.itemView, position)
            }

//            holder.itemView.checkView.setOnCheckedChangeListener { _, isChecked ->
//                if (isChecked) {
//                    list[position].checked = true
//                    presenter?.onItemChecked(holder.itemView, position)
//                }
//            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (list[position].name == "AddButton") {
            VIEW_TYPE_BOTTOM_ADD
        } else {
            VIEW_TYPE_ITEM
        }
    }

    fun add(bean: ProfileBean) {
        Log.d("112121->adapter", bean.name + "\n" + bean.profileValue)
        list.add(bean)
        notifyDataSetChanged()
    }

    fun hasItem(address: String?): Boolean {
        var flag = -1
        list.forEach {
            if (it.name == address) {
                flag = 1
            }
        }
        return flag == 1
    }

    fun update(itemBean: ProfileBean, position: Int) {
        var index1 = -1
        var index = -1
        for (i in 0 until list.size) {
            if (i != position && list[i].checked && list[i].name != itemBean.name) {
                index1 = i
                list[i].checked = false
            } else if (i == position && list[i].name == itemBean.name) {
                index = i
                if (!list[i].checked) {
                    list[i].checked = true
                }
                list[i].profileValue = itemBean.profileValue
                list[i].itemValues = itemBean.itemValues
            }
        }
        if (index1 != -1) {
            notifyItemChanged(index1)
        }
        if (index != -1) {
            notifyItemChanged(index)
        }
    }

    fun update(itemBean: ProfileBean, position: Int, itemPosition: Int) {
        var index1 = -1
        var index = -1
        for (i in 0 until list.size) {
            if (i != position && list[i].checked && list[i].name != itemBean.name) {
                index1 = i
                list[i].checked = false
            } else if (i == position && list[i].name == itemBean.name) {
                index = i
                if (!list[i].checked) {
                    list[i].checked = true
                }
                list[i].profileValue = itemBean.profileValue
                list[i].itemValues = itemBean.itemValues
                list[i].itemValues.forEach {
                    it.checked=false
                }
                list[i].itemValues[itemPosition].checked = true
            }
        }
        if (index1 != -1) {
            notifyItemChanged(index1)
        }
        if (index != -1) {
            notifyItemChanged(index)
        }
    }

    fun remove(position: Int) {
        list.removeAt(position)
        notifyItemRangeRemoved(position, 1)
        notifyItemRangeChanged(position, itemCount)
    }

    fun remove(deviceBean: ProfileBean) {
        var index = -1
        list.forEach {
            if (it.name == deviceBean.name) {
                index = list.indexOf(it)
                list.removeAt(index)
            }
        }
        if (index != -1) {
            notifyItemRangeRemoved(index, 1)
            notifyItemRangeChanged(index, itemCount)
        }
    }

    fun addAll(itList: MutableList<ProfileBean>) {
        list.addAll(itList)
        notifyDataSetChanged()
    }

    fun get(position: Int): ProfileBean {
        return list[position]
    }

    fun clearCheckPosition(position: Int) {
        var index = -1
        for (i in 0 until list.size) {
            if (i != position && list[i].checked) {
                index = i
                list[i].checked = false
            }
        }
        if (index != -1) {
            notifyItemChanged(index)
        }
    }

    fun checkPosition(position: Int) {
        var index = -1
        var index1 = -1
        for (i in 0 until list.size) {
            if (i == position && !list[i].checked) {
                index = i
                list[i].checked = true
            } else if (list[i].checked) {
                index1 = i
                list[i].checked = false
            }
        }
        if (index != -1) {
            notifyItemChanged(index)
        }
        if (index1 != -1) {
            notifyItemChanged(index1)
        }
    }

    fun clearChecked() {
        list.forEach {
            it.checked = false
        }
        notifyDataSetChanged()
    }

    fun clear() {
        list.clear()
        notifyDataSetChanged()
    }
}